﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Factory;
using BLL;
using System.Data;

namespace MyWorkspace
{
    public partial class CreateNewAuthority : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //将数据库中Level的信息写入DropDownList中
                IAuthorityRepository authoritymanage = AuthorityManageFactory.InitializeAuthorityRepository();
                //DropDownList 绑定DataTable
                this.Level.DataSource = authoritymanage.GetAllAuthorityLevelName();
                this.Level.DataTextField = "LevelName";
                this.Level.DataValueField = "LevelId";
                this.Level.DataBind();
                Level.Items.Insert(0, new ListItem("", ""));
            }

        }

        protected void New_Click(object sender, EventArgs e)
        {
            if (AuthorityName.Text.Length == 0)
            {
                Response.Write("<script>alert('AuthorityName is null!')</script>");
            }
            else
            {
                string newAuthorityName = AuthorityName.Text;//得到文本框输入的字符串
                string levelName = this.Level.SelectedItem.Text;//得到LeveName
                IAuthorityRepository authorityCreate = AuthorityManageFactory.InitializeAuthorityRepository();
                Authority authority = new Authority();
                authority.AuthorityName = newAuthorityName;
                authority.Level.LevelId = int.Parse(Level.SelectedValue);
                authority.Level.LevelName = Level.SelectedItem.Text;
                authorityCreate.AddNewAuthority(authority);
                Response.Redirect("AuthorityList.aspx");//页面跳转
            }
        }

    }
}